﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OFB.BLL.Context;
using OFB.Domain;
using System.Data;
using System.Data.SqlClient;

namespace OFB.BLL.Repository
{
    public class FunnyStoryBLL :IFunnyStoryBLL
    {
        public FunnyStoryContext context;

        public FunnyStoryBLL()
        {
            context = new FunnyStoryContext();
        }

        public ICollection<FunnyStory> FindAll(int startRow, int maxRow, out int totalRow)
        {
            IDataParameter para = context.FunnyStory.BuildParameter("totalRows", DbType.Int32, ParameterDirection.Output);
            totalRow = 0;
            ICollection<FunnyStory> lst = context.FunnyStory.Init("FunnyStory_Search", context.FunnyStory.BuildParameter("startRowIndex", DbType.Int32, startRow),
                                     context.FunnyStory.BuildParameter("maximumRows", DbType.Int32, maxRow),
                                     para)
                                               .MapCollection<FunnyStory>()
                                               .GetCollection();
            totalRow = Convert.ToInt32(para.Value);
            return lst;
        }

        public ICollection<FunnyStory> Filter(int startRow, int maxRow, out int totalRow,
                        string funnyStoryName = null, int? storyId = null, int? searchString = null)
        {
            IDataParameter para = context.FunnyStory.BuildParameter("totalRows", DbType.Int32, ParameterDirection.Output);
            totalRow = 0;
            ICollection<FunnyStory> lst = context.FunnyStory.Init("FunnyStory_Search",
                                        context.FunnyStory.BuildParameter("Name", DbType.String, funnyStoryName),
                                        context.FunnyStory.BuildParameter("StoryId", DbType.String, storyId),
                                        context.FunnyStory.BuildParameter("SearchString", DbType.String, searchString),
                                        context.FunnyStory.BuildParameter("startRowIndex", DbType.String, startRow),
                                        context.FunnyStory.BuildParameter("maximumRows", DbType.String, maxRow),
                                        para)
                                        .MapCollection<FunnyStory>()
                                        .GetCollection();
            totalRow = Convert.ToInt32(para.Value);
            return lst;
        }

        public int FunnyStoryCreate(string Title, string Content, bool IsDisabled)
        {
            IDataParameter funnyStoryIdParam = context.FunnyStory.BuildParameter("StoryId", DbType.Int32, ParameterDirection.Output);
            context.FunnyStory.Execute("FunnyStory_Create", context.FunnyStory.BuildParameter("Title", DbType.String, Title),
                                                    context.FunnyStory.BuildParameter("Content", DbType.String, Content),
                                                    context.FunnyStory.BuildParameter("IsDisabled", DbType.Boolean, IsDisabled),
                                                    funnyStoryIdParam);
            return (int)funnyStoryIdParam.Value;
        }

        public int FunnyStoryUpdate(int StoryId, string Title, string Content, bool IsDisabled)
        {
            IDataParameter rowEffectedParam = context.FunnyStory.BuildParameter("RowEffected", DbType.Int32, ParameterDirection.Output);
            context.FunnyStory.Execute("FunnyStory_Update", context.FunnyStory.BuildParameter("StoryId", DbType.Int32, StoryId),
                                                    context.FunnyStory.BuildParameter("Title", DbType.String, Title),
                                                    context.FunnyStory.BuildParameter("Content", DbType.String, Content),
                                                    context.FunnyStory.BuildParameter("IsDisabled", DbType.Boolean, IsDisabled),
                                                    rowEffectedParam);
            return (int) rowEffectedParam.Value;
        }

        public void FunnyStoryDisabled(int StoryId)
        {
            context.FunnyStory.Execute("FunnyStory_Disabled", context.FunnyStory.BuildParameter("StoryId", DbType.Int32, StoryId));
        }

        public FunnyStory Find(int StoryId)
        {
            return context.FunnyStory.Init("FunnyStory_Select", context.FunnyStory.BuildParameter("StoryId", System.Data.DbType.Int32, StoryId)).Map().Get();
        }

    }
}
